<?php
/**
 * Created by PhpStorm.
 * User: krystal
 * Date: 2019/3/22
 * Time: 15:32
 */

namespace app\common\model;

use think\Cache;

/**
 * 商品活动模型
 * Class ZhunleActivity
 * @package app\common\model
 */
class ZhunleActivity extends Base
{
    protected $table = 'wechat_backup_07_18_14_43_00.zhunle_activity';

    const ACTIVE_IDS = 'active:ids';//所有活动id缓存键名
    const ACTIVE_INFO_ID = 'active:info:id:';//活动信息缓存键名
    const CACHE_TIME = 86400 * 7;

    /**
     * 获取所有活动信息
     * @return array|bool
     */
    public function getALLActiveInfo()
    {
        $activeids = false;
        $data_list = [];

        if ($this->cache_on >= 1) {
            $activeids = Cache::get(self::ACTIVE_IDS);
        }

        if ($activeids === false) {
            try {
                $data = $this->where(['status' => 1])->column('id');
                if ($data) {
                    $activeids = implode(',', $data);
                }
                if ($this->cache_on >= 1 && $activeids) {
                    Cache::set(self::ACTIVE_IDS, $activeids, self::CACHE_TIME);
                }
            } catch (\Exception $e) {
                $this->error = '获取活动信息失败';
                return false;
            }
        }

        // 将课程章节id转为数组
        $activeid = explode(',', $activeids);


        // 根据当前页课程章节id获取所需的课程章节信息
        if (!empty($activeid)) {
            $ac_ids = '';
            $temp = [];

            // 获取缓存的课程章节内容,根据课程章节id
            foreach ($activeid as $ac_id) {
                if ($this->cache_on >= 1) {
                    // 根据课程章节id 获取课程章节内容
                    $temp[$ac_id] = Cache::get(self::ACTIVE_INFO_ID . $ac_id);
                } else {
                    // 缓存未开启时，默认从数据库获取
                    $temp[$ac_id] = false;
                }
                // 课程章节内容未缓存，等待从数据库中获取
                if ($temp[$ac_id] === false && $ac_id) {
                    $ac_ids .= $ac_id . ',';
                }
            }

            // 从数据库获取课程章节内容
            if ($ac_ids != '' || strlen($ac_ids) > 0) {
                //去掉结尾的 ，
                $ac_ids = substr($ac_ids, 0, strlen($ac_ids) - 1);

                $where = ['status' => 1,];

                try {
                    $data = $this
                        ->field('id,type_id,name,attr,start_time,end_time')
                        ->where($where)
                        ->where('id in(' . $ac_ids . ')')
                        ->order('sort DESC,create_time DESC')
                        ->select();

                    $course = json_decode(json_encode($data, true), true);
                } catch (\Exception $e) {
                    $this->error = '获取活动信息失败.';
                    return false;
                }

                if (!empty($course)) {
                    foreach ($course as $c) {
                        // 将课程章节内容缓存
                        if ($this->cache_on >= 1) {
                            Cache::set(self::ACTIVE_INFO_ID . $c['id'], $c, self::CACHE_TIME);
                        }
                        // 将数据库查出来的内容 写入到数组中为空的部分
                        if ($c) {
                            $temp[$c['id']] = $c;
                        }
                    }
                }
            }

            if (!empty($temp)) {
                foreach ($temp as $item) {
                    if ($item) {
                        $data_list[] = $item;
                    }
                }
            }
        }

        return $data_list;
    }

    /**
     * 获取某个活动的具体信息
     * @param array $map
     * @return bool|mixed
     */
    public function getActiveInfo($map = [])
    {
        $id = $map['id'];

        $data = false;

        if ($this->cache_on >= 1) {
            $data = Cache::get(self::ACTIVE_INFO_ID . $id);
        }

        if ($data === false) {
            $where = ['status' => 1, 'id' => $id];
            try {
                $action = $this
                    ->field('id,type_id,name,attr,start_time,end_time')
                    ->where($where)
                    ->findOrFail();
                $data = json_decode(json_encode($action, true), true);

                if ($this->cache_on >= 1) {
                    Cache::set(self::ACTIVE_INFO_ID . $id, $data, self::CACHE_TIME);
                }
            } catch (\Exception $e) {
                $this->error = '获取活动信息失败.';
                return false;
            }
        }

        return $data;
    }

}
